package com.icss.clothes.service.imp;

import com.icss.clothes.entity.Buy;
import com.icss.clothes.entity.Collection;
import com.icss.clothes.entity.Shopdetail;
import com.icss.clothes.mapper.BuyMapper;
import com.icss.clothes.mapper.ShopDetailMapper;
import com.icss.clothes.service.IBuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车的业务层实现类
 */
@Service
public class BuyService implements IBuyService {

    @Autowired
   private BuyMapper buyMapper;
    @Autowired
    private ShopDetailMapper shopDetailMapper;
    //添加购物车
    @Override
    public void addCar(Buy buy) {
        //需要一个能够通过指定的尺寸编号、颜色编号、商品编号获取得到商品详情信息的业务逻辑
        Shopdetail shopdetail= shopDetailMapper.selectAll(buy.getC_id(),buy.getSize_id(),buy.getColor_id());
        buy.setTotalPrice(shopdetail.getC_price()*buy.getQuantity());
        buy.setCd_id(shopdetail.getCd_id());
        buy.setC_picture1(shopdetail.getC_picture2());
        //查询要添加到购物车中的商品是否已经存在
     Buy sameBuy=buyMapper.findSameShopInShop(buy.getCd_id(),buy.getUser_id());
     if (sameBuy!=null){
         buyMapper.updateShopQuantity(sameBuy.getCar_id(),buy.getQuantity());
     }else{
         buyMapper.addCar(buy);
     }
    }
    //查看购物车
    @Override
    public List<Buy> findCarList(int user_id) {
        return buyMapper.findCarList(user_id);
    }

    @Override
    public void deleteCar(int car_id) {
        buyMapper.deleteCarByCar_id(car_id);
    }

    //用于头部购物车展示
    @Override
    public List<Buy> findCarListInTop(int user_id) {
        return buyMapper.findCarListInTop(user_id);
    }


}

